package com.fu.shop.service.Impl;

import com.fu.core.common.BusinessException;
import com.fu.core.common.ExceptionCode;
import com.fu.core.pojo.Order;
import com.fu.core.pojo.OrderDetailsVo;
import com.fu.core.pojo.OrderSku;
import com.fu.core.service.impl.BaseServiceImpl;
import com.fu.shop.mapper.OrderMapper;
import com.fu.shop.mapper.OrderSkuMapper;
import com.fu.shop.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author 付龙江
 * @date 2022/2/5 11:31
 */
@Service("orderService")
public class OrderServiceImpl extends BaseServiceImpl<Order> implements OrderService {


    OrderMapper orderMapper;
    OrderSkuMapper orderSkuMapper;
    @Autowired(required = false)
    public OrderServiceImpl(OrderMapper orderMapper,OrderSkuMapper orderSkuMapper) {
        this.orderMapper = orderMapper;
        this.orderSkuMapper = orderSkuMapper;
    }

    /**
     * 查询订单信息
     *
     * @param from          开始时间
     * @param to            结束时间
     * @param successStatus 成功状态
     * @param notPay        未支付
     * @param distribution  配送总
     * @param failedStatus  已取消
     * @param name          订单编号和用户名
     * @param page 页码
     * @param size 每页条数
     * @return 返回订单集合
     */
    @Override
    public PageInfo<Map<String, Object>> selectOrderList(String from, String to, Integer successStatus,
                                                         Integer notPay, Integer distribution, Integer failedStatus,
                                                         String name, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Map<String, Object>> list = orderMapper.selectOrderList(from, to, successStatus, notPay,
                distribution, failedStatus, name);
        return PageInfo.of(list);
    }

    /**
     * @return 导出订单简略信息
     */
    @Override
    public List<Map<String, Object>> uploadOrderList() {
        return orderMapper.selectOrderList(null, null, null, null,
                null, null, null);
    }

    @Override
    public OrderDetailsVo selectOrderDetails(Integer orderId) throws BusinessException {
        Order order = selectByPk(orderId);
        OrderDetailsVo orderDetailsVo = new OrderDetailsVo();
        orderDetailsVo.setOrder(order);
        Example example = new Example(OrderSku.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        List<OrderSku> orderSkuList = orderSkuMapper.selectByExample(example);
        orderDetailsVo.setOrderSkus(orderSkuList);
        return orderDetailsVo;
    }

    /**
     * 取消订单
     * 先判断订单是否完成
     * 完成了的订单无法取消
     *
     * @param orderId 点单编号
     * @throws Exception 异常处理
     */
    @Override
    public void cancelOrder(Integer orderId) throws Exception {
        Order order = selectByPk(orderId);
        if (order.getOrderStatus() == 1){
            //订单已完成
            throw new BusinessException(ExceptionCode.ORDER_CANCEL_FILED);
        }
        //订单未完成，取消订单
        order.setOrderStatus(4);
        updateTByNewT(order);
    }
}
